package dal;

import java.util.ArrayList;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import entities.Movie;
import entities.Theater;

public class ManageMovie
{
	private SessionFactory		factory;
	private static ManageMovie	instance;

	@SuppressWarnings("deprecation")
	private ManageMovie()
	{
		try
		{
			factory = new Configuration().configure().buildSessionFactory();
		} catch (Throwable ex)
		{
			System.err.println("Failed to create sessionFactory object." + ex);
			throw new ExceptionInInitializerError(ex);
		}
	}

	public static ManageMovie getInstance()
	{
		if (instance == null)
		{
			instance = new ManageMovie();
		}

		return instance;
	}

	public ArrayList<Integer> add(ArrayList<Movie> moviesToAdd)
	{
		ArrayList<Integer> IDsAdded = new ArrayList<Integer>();

		for (Movie movieToAdd : moviesToAdd)
		{
			IDsAdded.add(this.add(movieToAdd));
		}

		return IDsAdded;
	}

	public Integer add(Movie movieToAdd)
	{
		Session session = factory.openSession();
		Transaction tx = null;
		Integer movieID = null;

		try
		{
			tx = session.beginTransaction();
			movieID = (Integer) session.save(movieToAdd);
			tx.commit();
		} catch (HibernateException e)
		{
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
		} finally
		{
			session.close();
		}
		return movieID;
	}

	@SuppressWarnings("unchecked")
	public ArrayList<Movie> getBy(Theater byTheater)
	{
		ArrayList<Movie> movies = null;
		Session session = factory.openSession();
		Transaction tx = null;
		Query q = null;

		try
		{
			tx = session.beginTransaction();
			q = session
					.createQuery("SELECT m"
							+ " FROM entities.Movie m,entities.Show s,entities.Theater t WHERE"
							+ " t.id = s.showTheater and m.id = s.showMovie and t.id = :t");
			q.setParameter("t", byTheater.getID());

			movies = (ArrayList<Movie>) q.list();
			tx.commit();
		} catch (HibernateException e)
		{
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
		} finally
		{
			session.close();
		}
		return movies;
	}

	@SuppressWarnings("unchecked")
	public ArrayList<Movie> getAll()
	{
		ArrayList<Movie> movies = null;
		Session session = factory.openSession();
		Transaction tx = null;
		try
		{
			tx = session.beginTransaction();
			movies = (ArrayList<Movie>) session.createQuery(
					"FROM entities.Movie").list();
			tx.commit();
		} catch (HibernateException e)
		{
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
		} finally
		{
			session.close();
		}
		return movies;
	}

	public void update(Integer MovieID, String name)
	{
		Session session = factory.openSession();
		Transaction tx = null;
		try
		{
			tx = session.beginTransaction();
			Movie movie = (Movie) session.get(Movie.class, MovieID);
			movie.setMovieName(name);
			session.update(movie);
			tx.commit();
		} catch (HibernateException e)
		{
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
		} finally
		{
			session.close();
		}
	}

	public void delete(Integer movieID)
	{
		Session session = factory.openSession();
		Transaction tx = null;
		try
		{
			tx = session.beginTransaction();
			Movie movie = (Movie) session.get(Movie.class, movieID);
			session.delete(movie);
			tx.commit();
		} catch (HibernateException e)
		{
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
		} finally
		{
			session.close();
		}
	}
}
